package com.example.weatherpredict.base;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.KeyboardUtils;


/**
 * BaseActivity
 * description: TODO
 *
 * @author : Licy
 * @date : 2020/8/13
 * email ：licy3051@qq.com
 */
public abstract class BaseActivity<VB extends ViewDataBinding> extends AppCompatActivity {

    protected BaseActivity mActivity;
    protected VB mViewBinding;


    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initListener();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (DeviceUtils.isTablet()) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        }

        mActivity = this;

        mViewBinding = DataBindingUtil.setContentView(this, getLayoutId());
        mViewBinding.setLifecycleOwner(this);

        initView();
        initListener();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mViewBinding != null) {
            mViewBinding.unbind();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        KeyboardUtils.hideSoftInput(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        KeyboardUtils.hideSoftInput(this);
    }


    @ColorInt
    public static int getColor(Context context, @ColorRes int id) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return ContextCompat.getColor(context, id);
        } else {
            return context.getResources().getColor(id);
        }
    }

}